class Util {
	public constructor() {
	}
	//获取存取的值
	public static getValue(array, key): string {
		let value = "";
		for (let i = 0; i < array.length; i++) {
			let dt = array[i];
			if (dt["key"] == key) {
				return dt["value"];
			}
		}
		return value;
	}
	/**返回当前是今年第几周---每周从周一开始 */
    public static getWeekOfYear() {
        var today = new Date();
        var firstDay = new Date(today.getFullYear(), 0, 1);
        var dayOfWeek = firstDay.getDay();
        var spendDay = 1;
        if (dayOfWeek != 0) {
            spendDay = 7 - dayOfWeek + 1;
        }
        firstDay = new Date(today.getFullYear(), 0, 1 + spendDay);
        var d = Math.ceil((today.valueOf() - firstDay.valueOf()) / 86400000);
        var result = Math.ceil(d / 7);
        return result + 1;
    };

	public static deleteRankData(list:Array<any>,keyValues:Array<any>,userId)
	{
        let mineItem:UserInfo = null;
        let delList:Array<UserInfo> = [];
		for (let i = 0; i < list.length; i++) {
            let item:any = list[i];
            if (item.KVDataList.length == keyValues.length) {
                let userInfo:UserInfo = Util.converItem2UserInfo(item,keyValues,userId);
                if(userInfo.isMine){
                    mineItem = userInfo;
                }
                delList.push(userInfo);
            }
        }
        return {list:delList,mineItem:mineItem};
	}
    public static converItem2UserInfo(item:any,keyValues:Array<any>,userId):any{
        
        // 将数据内容根据keyvalue设置成数据
        for(let i=0;i<keyValues.length;i++){
            let kvItem = keyValues[i];
            let fromKey = kvItem["fromKey"];
            let toKey = kvItem["toKey"];
            for(let j=0;j<item.KVDataList.length;j++){
                let kvDataItem = item.KVDataList[j];                       
                if( kvDataItem.key == fromKey){
                    item[toKey] = kvDataItem.value;
                    break;
                }
            }
        }

        if(item.id == userId){
            item.isMine = true;
        }else{
            item.isMine = false;
        }

        return item;
    }

	//排序
    public static sortScoreData(list: Array<UserInfo>,sortKey:string):Array<UserInfo> {
        //排序
        let len = list.length;
        for (let i = 0; i < len; i++) {
            for (let j = 0; j < len - i; j++) {
                let currentItem = list[j];
                let score1 = parseInt(currentItem[sortKey]);
                let score2 = 0;
                let nextItem = null;
                if(j+1<len){
                    nextItem = list[j+1];
                    score2 = parseInt(nextItem[sortKey])
                }
                if (score1 < score2) {
                    let temp = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = temp;
                }
            }
        }
        /**添加上排名 */
        for(let i=0;i<list.length;i++){
            list[i].rank = i+1;
        }
        return list;
    }
}